package com.qumitech.visualnovel.service.account.impl;

import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.component.util.ExcelUtils;
import com.qumitech.visualnovel.component.util.FileUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.datasource.console.AccountDailyActiveMapper;
import com.qumitech.visualnovel.datasource.console.AccountGroupConfigMapper;
import com.qumitech.visualnovel.datasource.console.AccountGroupInfoMapper;
import com.qumitech.visualnovel.datasource.console.AccountInactiveInfoMapper;
import com.qumitech.visualnovel.model.dao.app.Account;
import com.qumitech.visualnovel.model.dao.console.AccountDailyActive;
import com.qumitech.visualnovel.model.dao.console.AccountGroupConfig;
import com.qumitech.visualnovel.service.account.AccountGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author zhangbc
 * @version 1.0.0
 * @date 2020/7/17 15:47
 **/
@Service
public class AccountGroupServiceImpl implements AccountGroupService {

    @Autowired
    AccountGroupConfigMapper groupConfigMapper;

    @Autowired
    AccountGroupInfoMapper groupInfoMapper;

    @Autowired
    AccountInactiveInfoMapper inactiveInfoMapper;

    @Autowired
    AccountDailyActiveMapper activeMapper;

    @Override
    public List<HashMap<String,Object>> getAccountGroups() {
        return groupConfigMapper.getGroupOptions();
    }

    @Override
    public List<AccountGroupConfig> listAccountGroupInfoData(Integer levelId, String startTime, String endTime,
                                                             int offset, int pageSize) {
        return groupConfigMapper.listAccountGroupInfoData(levelId, startTime, endTime, offset, pageSize);
    }

    @Override
    public int getCount(Integer levelId, String startTime, String endTime) {
        return groupConfigMapper.getCount(levelId, startTime, endTime);
    }

    @Override
    public List<Integer> getUserIds(Integer userGroup) {
        List<Integer> userIds;
        int group = 2;
        if (userGroup == 1) {
            userIds = groupInfoMapper.getAccounts(1);
        } else if (userGroup == group) {
            userIds = groupInfoMapper.getAccounts(0);
        } else {
            userIds = inactiveInfoMapper.getAccounts(userGroup - 3);
        }

        return userIds;
    }

    @Override
    public List<String> getGroups(Integer userGroup, int size) {
        List<Integer> uids = getUserIds(userGroup);
        List<String> arrays = new ArrayList<>();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < uids.size(); i++) {
            sb.append(uids.get(i));
            if ((i + 1) % size == 0) {
                arrays.add(sb.toString());
                sb = new StringBuffer();
            } else if (i == uids.size() - 1) {
                arrays.add(sb.toString());
            } else {
                sb.append(',');
            }
        }
        return arrays;
    }

    @Override
    public AccountGroupConfig findGroupConfigById(Long id) {
        return groupConfigMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<AccountDailyActive> listAccountDailyActive(Integer groupId, String startTime, String endTime,
                                                           int offset, int pageSize) {
        if (groupId == null) {
            groupId = 1;
        }

        List<Integer> userIds = getUserIds(groupId);
        return activeMapper.getList(userIds, startTime, endTime, offset, pageSize);
    }

    @Override
    public int getAccountActiveCount(Integer groupId, String startTime, String endTime) {
        if (groupId == null) {
            groupId = 1;
        }

        List<Integer> userIds = getUserIds(groupId);
        return activeMapper.getCount(userIds, startTime, endTime);
    }

    @Override
    public Map<String, Object> listGroupActiveInfoExport(Integer groupId, String startTime, String endTime,
                                                         Integer offset, Integer pageSize,
                                                         HttpServletResponse response) {
        Map<String, Object> resultMap = new HashMap<>(16);
        try {
            List<AccountDailyActive> actives = listAccountDailyActive(groupId, startTime, endTime, offset, pageSize);
            ExcelUtils<AccountDailyActive> xls = new ExcelUtils<>(AccountDailyActive.class);
            String filename = FileUtil.getExcelName();
            response.reset();
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + filename);
            OutputStream outputStream = response.getOutputStream();
            xls.exportExcel("分组用户活跃信息", actives, outputStream);
            outputStream.flush();
            outputStream.close();
            resultMap.put("title", filename);
            return resultMap;
        } catch (IOException e) {
            e.printStackTrace();
        }

        throw new BusinessException(ResultEnum.EXCEL_EXPORT_ERROR);
    }
}
